<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 配置管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        :root {
            --bg-primary: #0a0a0a;
            --bg-secondary: #1a1a1a;
            --bg-tertiary: #2a2a2a;
            --text-primary: #ffffff;
            --text-secondary: #a0a0a0;
            --text-tertiary: #606060;
            --border-color: #333333;
            --accent-color: #ffffff;
            --error-color: #ff4444;
            --success-color: #00ff00;
        }
        
        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
        }
        
        /* 背景网格效果 */
        body::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-image: 
                linear-gradient(var(--border-color) 1px, transparent 1px),
                linear-gradient(90deg, var(--border-color) 1px, transparent 1px);
            background-size: 50px 50px;
            opacity: 0.1;
            z-index: 0;
        }
        
        .login-container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 450px;
            padding: 20px;
        }
        
        .login-box {
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            padding: 60px 50px;
            position: relative;
        }
        
        /* 装饰性边角 */
        .login-box::before,
        .login-box::after {
            content: '';
            position: absolute;
            width: 20px;
            height: 20px;
            border: 2px solid var(--accent-color);
        }
        
        .login-box::before {
            top: -1px;
            left: -1px;
            border-right: none;
            border-bottom: none;
        }
        
        .login-box::after {
            bottom: -1px;
            right: -1px;
            border-left: none;
            border-top: none;
        }
        
        .logo-section {
            text-align: center;
            margin-bottom: 50px;
        }
        
        .logo {
            width: 60px;
            height: 60px;
            margin: 0 auto 20px;
            border: 2px solid var(--accent-color);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: bold;
            letter-spacing: 2px;
        }
        
        .system-title {
            font-size: 24px;
            font-weight: 300;
            letter-spacing: 4px;
            margin-bottom: 10px;
            text-transform: uppercase;
        }
        
        .system-subtitle {
            font-size: 12px;
            color: var(--text-secondary);
            letter-spacing: 2px;
            text-transform: uppercase;
        }
        
        .form-group {
            margin-bottom: 30px;
            position: relative;
        }
        
        .form-label {
            display: block;
            font-size: 11px;
            color: var(--text-secondary);
            margin-bottom: 10px;
            letter-spacing: 2px;
            text-transform: uppercase;
        }
        
        .form-input {
            width: 100%;
            background: var(--bg-primary);
            border: 1px solid var(--border-color);
            color: var(--text-primary);
            padding: 15px 20px;
            font-size: 14px;
            transition: all 0.3s;
            font-family: inherit;
        }
        
        .form-input:focus {
            outline: none;
            border-color: var(--accent-color);
            background: var(--bg-tertiary);
        }
        
        .form-input::placeholder {
            color: var(--text-tertiary);
        }
        
        .btn-login {
            width: 100%;
            background: var(--accent-color);
            color: var(--bg-primary);
            border: none;
            padding: 15px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 3px;
            text-transform: uppercase;
            cursor: pointer;
            transition: all 0.3s;
            position: relative;
            overflow: hidden;
        }
        
        .btn-login::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
            transition: left 0.5s;
        }
        
        .btn-login:hover::before {
            left: 100%;
        }
        
        .btn-login:hover {
            background: var(--text-primary);
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(255,255,255,0.2);
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        .btn-login:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        .message {
            padding: 15px;
            margin-bottom: 25px;
            font-size: 12px;
            border-left: 3px solid;
            display: none;
        }
        
        .message.error {
            background: rgba(255, 68, 68, 0.1);
            border-color: var(--error-color);
            color: var(--error-color);
        }
        
        .message.success {
            background: rgba(0, 255, 0, 0.1);
            border-color: var(--success-color);
            color: var(--success-color);
        }
        
        .footer {
            text-align: center;
            margin-top: 40px;
            font-size: 11px;
            color: var(--text-tertiary);
            letter-spacing: 1px;
        }
        
        .loading {
            display: inline-block;
            width: 14px;
            height: 14px;
            border: 2px solid var(--bg-primary);
            border-top-color: transparent;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 8px;
            vertical-align: middle;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        /* 响应式 */
        @media (max-width: 768px) {
            .login-box {
                padding: 40px 30px;
            }
            
            .system-title {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-box">
            <div class="logo-section">
                <div class="logo">CM</div>
                <h1 class="system-title">Config Manager</h1>
                <p class="system-subtitle">Configuration Management System</p>
            </div>
            
            <div id="message" class="message"></div>
            
            <form id="loginForm">
                <div class="form-group">
                    <label class="form-label" for="username">Username</label>
                    <input 
                        type="text" 
                        id="username" 
                        class="form-input" 
                        placeholder="Enter your username"
                        autocomplete="username"
                        required
                    >
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="password">Password</label>
                    <input 
                        type="password" 
                        id="password" 
                        class="form-input" 
                        placeholder="Enter your password"
                        autocomplete="current-password"
                        required
                    >
                </div>
                
                <button type="submit" class="btn-login" id="loginBtn">
                    <span id="btnText">Login</span>
                </button>
            </form>
            
            <div class="footer">
                © 2025 Config Management System v2.0
            </div>
        </div>
    </div>
    
    <script>
        const API_BASE = 'http://localhost:3000';
        
        function showMessage(text, type) {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = 'message ' + type;
            messageEl.style.display = 'block';
            
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 5000);
        }
        
        function setLoading(loading) {
            const btn = document.getElementById('loginBtn');
            const btnText = document.getElementById('btnText');
            
            if (loading) {
                btn.disabled = true;
                btnText.innerHTML = '<span class="loading"></span>Processing...';
            } else {
                btn.disabled = false;
                btnText.textContent = 'Login';
            }
        }
        
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                showMessage('请填写用户名和密码', 'error');
                return;
            }
            
            setLoading(true);
            
            try {
                const response = await fetch(API_BASE + '/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include',
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                
                if (data.errcode === 0) {
                    // 保存 token
                    localStorage.setItem('token', data.data.token);
                    localStorage.setItem('user', JSON.stringify(data.data.user));
                    
                    showMessage('登录成功,正在跳转...', 'success');
                    
                    setTimeout(() => {
                        window.location.href = 'dashboard.html';
                    }, 1000);
                } else {
                    showMessage(data.message || '登录失败', 'error');
                    setLoading(false);
                }
            } catch (error) {
                console.error('Login error:', error);
                showMessage('网络错误,请检查服务器连接', 'error');
                setLoading(false);
            }
        });
        
        // 检查是否已登录
        window.addEventListener('load', () => {
            const token = localStorage.getItem('token');
            if (token) {
                // 验证 token 是否有效
                fetch(API_BASE + '/api/auth/me', {
                    headers: {
                        'Authorization': 'Bearer ' + token
                    }
                })
                .then(res => res.json())
                .then(data => {
                    if (data.errcode === 0) {
                        window.location.href = 'dashboard.html';
                    }
                })
                .catch(() => {
                    // Token 无效,清除
                    localStorage.removeItem('token');
                    localStorage.removeItem('user');
                });
            }
        });
    </script>
</body>
</html>

